package com.study.javaBasic.array;

import org.apache.commons.lang.time.StopWatch;

import java.util.ArrayList;
import java.util.LinkedList;

public class ForAndForEach {
    public static void main(String[] args) {
        int times=100000;
        StopWatch stopWatch=new StopWatch();
        ArrayList<Integer> alist = new ArrayList<Integer>();
        LinkedList<Integer> linkList  = new LinkedList<Integer>();
        for (int i =0;i<times;i++){
            alist.add(i);
            linkList.add(i);
        }
        stopWatch.start();
        for (int i=0;i<times;i++){
            Integer y=alist.get(i);
        }
        stopWatch.split();
        System.out.println("For ArrayList耗时:"+stopWatch.getSplitTime());
        stopWatch.reset();
        stopWatch.start();
        for (Integer i:alist){
            Integer y=i;
        }
        stopWatch.split();
        System.out.println("Foreach ArrayList耗时:"+stopWatch.getSplitTime());
        stopWatch.reset();
        stopWatch.start();
        for (int i=0;i<times;i++){
            Integer y=linkList.get(i);
        }
        stopWatch.split();
        System.out.println("For linkList耗时:"+stopWatch.getSplitTime());
        stopWatch.reset();
        stopWatch.start();
        for (Integer i:linkList){
            Integer y=i;
        }
        stopWatch.split();
        System.out.println("Foreach linkList耗时:"+stopWatch.getSplitTime());
    }
}
